﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace NewsWcfWebService
{
     class tuoitreGetInfo
    {
        static string pageUrl = "http://tuoitre.vn";
        static htmlParse request = new htmlParse(pageUrl);
        static HtmlNode root = request.HtmlDoc.DocumentNode.SelectSingleNode(".//div[@id='header']/div[@class='mnuBartwo']/ul");
        static HtmlNodeCollection listNodeCatelogesMenu = root.ChildNodes;
        static ResourceLoader resource = new ResourceLoader();
        public static List<ItemNewsInfo> getSubCatelogesMenuOfNode (HtmlNode node)
        {
            ItemNewsInfo info = new ItemNewsInfo();
            HtmlNodeCollection listNode = node.SelectNodes(".//li");
            List<ItemNewsInfo> listCateloges = new List<ItemNewsInfo>();
            if (listNode != null)
            {
                for (int i = 0; i < listNode.Count; i++)
                {
                    ItemNewsInfo infoItem = new ItemNewsInfo();
                    HtmlNode childNode = listNode[i];

                    HtmlAttributeCollection attribute = childNode.FirstChild.Attributes;
                    infoItem.Url = attribute["href"].Value.ToString();
                    if (StringUtils.getFirstCharOfString(infoItem.Url) == '/')
                    {
                        infoItem.Url = StringUtils.combineTwoString(pageUrl, infoItem.Url);
                    }

                    infoItem.Tilte = childNode.FirstChild.InnerText.Trim();
                    infoItem.KeyId = ResourceLoader.getInstance().getIdOfKeyName(infoItem.Tilte);
                    infoItem.Type = 0x01;

                    listCateloges.Add(infoItem);
                }
            }
            return listCateloges;

        }
        public static List<ItemNewsInfo> getCatelogesMenu()
        {
            List<ItemNewsInfo> list = new List<ItemNewsInfo>();
            List<HtmlNode> listRecycle = new List<HtmlNode>();
            for (int i = 0; i < listNodeCatelogesMenu.Count; i++)
            {
                HtmlNode node = listNodeCatelogesMenu[i];
                if (node.FirstChild != null && !(node.FirstChild.InnerText.Trim().CompareTo("") == 0))
                {
                    ItemNewsInfo infoItem = new ItemNewsInfo();

                    infoItem.Url = getUrlOfNode(node);
                    infoItem.Url = StringUtils.formatStringWithChar(infoItem.Url, "\"");

                    infoItem.Tilte = node.FirstChild.InnerText.Trim();
                    infoItem.KeyId = ResourceLoader.getInstance().getIdOfKeyName(infoItem.Tilte);

                    infoItem.Type = 0x01;
                    list.Add(infoItem);
                }
                else
                {
                    listRecycle.Add(node);
                }
            }
            foreach (HtmlNode reNode in listRecycle)
            {
                listNodeCatelogesMenu.Remove(reNode);
            }
            return list;
        }
        public static List<ItemNewsInfo> getSubCatelogesMenuWithUrl(string url)
        {
            for (int i = 0; i < listNodeCatelogesMenu.Count; i++)
            {
                HtmlNode node = listNodeCatelogesMenu[i];
                if (node.FirstChild != null && !(node.FirstChild.InnerText.Trim().CompareTo("") == 0))
                {
                    string nodeUrl = getUrlOfNode(node);
                    if (nodeUrl.CompareTo(url) == 0)
                    {
                        return getSubCatelogesMenuOfNode(node);
                    }
                }      
            }
            return null;
        }
        public static string getUrlOfNode(HtmlNode node)
        {
            HtmlAttributeCollection attribute = node.FirstChild.Attributes;
            string result = attribute["href"].Value.ToString();
            if (StringUtils.getFirstCharOfString(result) == '/')
            {
                result = StringUtils.combineTwoString(pageUrl, result);
            }
            return result;
        }
        public static List<ItemNewsInfo> getNewsListWithUrl(string url)
        {
            List<ItemNewsInfo> list = new List<ItemNewsInfo>();
            htmlParse htmlRequest = new htmlParse(url);
            HtmlNode newsRoot = htmlRequest.HtmlDoc.DocumentNode.SelectSingleNode(".//ul[@class='listItemGrNewsBox']");
            HtmlNodeCollection listNodeNews = newsRoot.ChildNodes;
            List<HtmlNode> listRecycle = new List<HtmlNode>();
            for (int i = 0; i < listNodeNews.Count; i++)
            {
                HtmlNode node = listNodeNews[i];
                if (node.FirstChild != null)
                {
                    ItemNewsInfo infoItem = new ItemNewsInfo();

                    HtmlNode infoNode= node.SelectSingleNode(".//h3/a");
                    infoItem.Url = infoNode.Attributes["href"].Value.ToString();
            
                    infoItem.Tilte = infoNode.FirstChild.InnerText.Trim();
                    infoItem.KeyId = ResourceLoader.getInstance().getIdOfKeyName(infoItem.Tilte);
                    infoItem.Type = 0x02;
                    list.Add(infoItem);
                }
                else
                {
                    listRecycle.Add(node);
                }
            }
            foreach (HtmlNode reNode in listRecycle)
            {
                listNodeNews.Remove(reNode);
            }
            return list;
        }
        public static ItemNewsInfo getContentNewsFromUrl(string url)
        {
            List<string> list = new List<string>();
            ItemNewsInfo info = new ItemNewsInfo();
            htmlParse htmlRequest = new htmlParse(url);
            HtmlNode titleNode = htmlRequest.HtmlDoc.DocumentNode.SelectSingleNode(".//h1");
            info.Tilte = titleNode.InnerText.Trim();
            info.KeyId = ResourceLoader.getInstance().getIdOfKeyName(info.Tilte);
            info.Url = url;
            HtmlNodeCollection listSource = htmlRequest.HtmlDoc.DocumentNode.SelectNodes(".//div[@class='padR10']/div[@class='padT10']");
            HtmlNode timeStampNode = listSource[0];
            info.TimeStamp = timeStampNode.InnerText.Trim();
            info.Type = 0x03;
            HtmlNode subNode = listSource[1];
            string textDecription = subNode.InnerText.Trim();
            list.Add(textDecription);
            HtmlNode imageNode = subNode.SelectSingleNode(".//img");
            string imageSrc = imageNode.Attributes[0].Value.ToString();
            list.Add(imageSrc);
            HtmlNode newsRoot = htmlRequest.HtmlDoc.DocumentNode.SelectSingleNode(".//div[@class='colitemDetailNewsRight']/div[@class='padB10']");
            HtmlNodeCollection listNodeNews = newsRoot.ChildNodes;
            for (int i = 0; i < listNodeNews.Count; i++)
            {
                HtmlNode node = listNodeNews[i];
                if (node.FirstChild != null)
                {
                    string text = node.InnerText.Trim();
                    list.Add(text);
                }

            }
            info.MessageContent = list;
            return info;
        }
    }
}
